const router = require('koa-router')()
const koaBody = require('koa-body')();
const request = require('superagent'); //不要忘记npm install

module.exports = router.post('/api/cms/topics/query', koaBody,(ctx) => {
    console.log(ctx.request.body);
    // => POST body
    ctx.body = JSON.stringify(ctx.request.body);
    console.log(ctx.request.body.name)
    // 调用远程接口获取用户授权信息
    request
    .post(config.host+'/api/cms/topics/query')
    .send({ code: 'Manny', merchantNo: 'wwew' })
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json')
    .end(function(err, res) {
        var json = eval("("+res.text+")");
        if (json.ok) {
            console.log('yay got ' + JSON.stringify(res.body));
            ctx.render('index', { title: 'Nunjucks', content: 'Feifeiyu yeah!' });
        } else {
            console.log('Oh no! error ' + err);
        }
    });
  }).post('/api/cms/topics/query2', koaBody,(ctx) => {
    console.log(ctx.request.body);
    // => POST body
    ctx.body = JSON.stringify(ctx.request.body);
    console.log(ctx.body)
    // 调用远程接口获取用户授权信息
    request
    .post(config.host+'/api/cms/topics/query')
    .send({ code: 'Manny', merchantNo: 'wwew' })
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json')
    .end(function(err, res) {
        var json = eval("("+res.text+")");
        if (json.ok) {
            console.log('yay got ' + JSON.stringify(res.body));
            ctx.render('index', { title: 'Nunjucks', content: 'Feifeiyu yeah!' });
        } else {
            console.log('Oh no! error ' + err);
        }
    });
  });
